/**
 * 
 */
package hm.base.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.trendytech.tds.common.bean.Pager;

import hm.base.dao.IBaseDAO;
import hm.base.service.IBaseService;
import hm.entity.AbstractEntity;

/**
 * @author Hai
 *
 */
public abstract class AbstractService<T> implements IBaseService<T> {

	protected Log logger = LogFactory.getLog(getClass());
	
	private static final String MAPPER_SELECT_ALL = "selectAll";
	
	protected IBaseDAO<T> baseDAO;
	public void setBaseDAO(IBaseDAO<T> baseDAO) {
		this.baseDAO = baseDAO;
	}

	public IBaseDAO<T> getBaseDAO() {
		return baseDAO;
	}
	
	@Override
	public int insert(T entity) {
		return baseDAO.insert(entity);
	}
	
	@Override
	public int update(T entity) {
		return baseDAO.update(entity);
	}
	
	@Override
	public int save(T entity) {
		int result = 0;
		if (entity instanceof AbstractEntity) {
			AbstractEntity model = (AbstractEntity) entity;
			if (StringUtils.isNotEmpty(model.getId())) {
				result = baseDAO.update(entity);
			} else {
				result = baseDAO.insert(entity);
			}
		}
		return result;
	}

	@Override
	public List<T> getAll() {
		return baseDAO.getList(MAPPER_SELECT_ALL, null);
	}

	@Override
	public T getById(String id) {
		return baseDAO.getById(id);
	}

	@Override
	public List<T> getByPager(Pager<T> pager) {
		return baseDAO.getByPager(pager);
	}

	@Override
	public int delete(String id) {
		return baseDAO.delete(id);
	}

	@Override
	public int deleteLogical(T entity) {
		return baseDAO.deleteLogical(entity);
	}

}
